package com.gx.controller;

import com.gx.aspect.annotation.OperationLogger;
import com.gx.domain.SysDataDict;
import com.gx.page.TableDataInfo;
import com.gx.response.R;
import com.gx.service.api.SysDataDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dicts")
public class DictController extends BaseController {

    @Autowired private SysDataDictService sysDataDictService;

    @GetMapping
    public R list(@RequestParam(value = "roleSessionName", defaultValue = "", required = false) String roleSessionName,
                  @RequestParam(value = "fieldCode", defaultValue = "", required = false) String fieldCode,
                  @RequestParam(value = "itemName", defaultValue = "", required = false) String itemName
    ) {
        startPage();
        SysDataDict sysDataDict = new SysDataDict();
        sysDataDict.setRoleSessionName(roleSessionName);
        sysDataDict.setFieldCode(fieldCode);
        sysDataDict.setItemName(itemName);
        List<SysDataDict> dictList = sysDataDictService.queryList(sysDataDict);
        TableDataInfo dataTable = getDataTable(dictList);
        return R.ok().put("rows", dataTable.getRows()).put("total",dataTable.getTotal());
    }

    @OperationLogger(option = "新增字典")
    @PostMapping
    public R add(@RequestBody SysDataDict sysDataDict) {
        sysDataDictService.save(sysDataDict);
        return R.ok("新增成功");
    }

    @OperationLogger(option = "修改字典")
    @PutMapping
    public R update(@RequestBody SysDataDict sysDataDict){
        sysDataDictService.update(sysDataDict);
        return R.ok("更新成功");
    }

//    @OperationLogger(option = "删除字典")
//    @DeleteMapping("/{id}")
//    public R delete(@PathVariable("id") Long id) {
//        sysDataDictService.delete(id);
//        return R.ok("删除成功");
//    }

    @GetMapping("/selectTestAAATree")
    public R tree() {
        return R.ok().put("tree","");
    }
}
